/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.source.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.utils.I18nUtils;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.mapper.JaxbMapper;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.FileUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.source.entity.EleSourceGroup;
import com.thinkgem.jeesite.modules.source.entity.EleSourceMultimedia;
import com.thinkgem.jeesite.modules.source.entity.alarm.EleSourceAlarm;
import com.thinkgem.jeesite.modules.source.entity.alarm.EleSourceAlarmGroup;
import com.thinkgem.jeesite.modules.source.entity.background.EleSourceBackground;
import com.thinkgem.jeesite.modules.source.entity.direction.EleSourceDirection;
import com.thinkgem.jeesite.modules.source.entity.direction.EleSourceDirectionGroup;
import com.thinkgem.jeesite.modules.source.entity.floor.EleSourceFloor;
import com.thinkgem.jeesite.modules.source.entity.floor.EleSourceFloored;
import com.thinkgem.jeesite.modules.source.entity.floor.Floor;
import com.thinkgem.jeesite.modules.source.entity.floor.FloorNode;
import com.thinkgem.jeesite.modules.source.entity.font.EleSourceFont;
import com.thinkgem.jeesite.modules.source.entity.image.EleSourceImage;
import com.thinkgem.jeesite.modules.source.entity.image.EleSourceImageGroup;
import com.thinkgem.jeesite.modules.source.entity.subtitle.EleSourceSubtitleGroup;
import com.thinkgem.jeesite.modules.source.entity.time.EleSourceDate;
import com.thinkgem.jeesite.modules.source.entity.time.EleSourceTime;
import com.thinkgem.jeesite.modules.source.entity.title.EleSourceTitle;
import com.thinkgem.jeesite.modules.source.entity.video.EleSourceVideo;
import com.thinkgem.jeesite.modules.source.entity.video.EleSourceVideoGroup;
import com.thinkgem.jeesite.modules.source.entity.weather.EleSourceWeather;
import com.thinkgem.jeesite.modules.source.entity.week.EleSourceWeek;
import com.thinkgem.jeesite.modules.source.service.alarm.EleSourceAlarmGroupService;
import com.thinkgem.jeesite.modules.source.service.background.EleSourceBackgroundService;
import com.thinkgem.jeesite.modules.source.service.direction.EleSourceDirectionGroupService;
import com.thinkgem.jeesite.modules.source.service.floor.EleSourceFloorService;
import com.thinkgem.jeesite.modules.source.service.floor.EleSourceFlooredService;
import com.thinkgem.jeesite.modules.source.service.floor.FloorNodeService;
import com.thinkgem.jeesite.modules.source.service.image.EleSourceImageGroupService;
import com.thinkgem.jeesite.modules.source.service.subtitle.EleSourceSubtitleGroupService;
import com.thinkgem.jeesite.modules.source.service.time.EleSourceDateService;
import com.thinkgem.jeesite.modules.source.service.time.EleSourceTimeService;
import com.thinkgem.jeesite.modules.source.service.title.EleSourceTitleService;
import com.thinkgem.jeesite.modules.source.service.video.EleSourceVideoGroupService;
import com.thinkgem.jeesite.modules.source.service.weather.EleSourceWeatherService;
import com.thinkgem.jeesite.modules.source.service.week.EleSourceWeekService;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.service.SystemService;
import com.thinkgem.jeesite.modules.templet.entity.EleTemplet;
import com.thinkgem.jeesite.modules.templet.service.EleTempletService;
import com.thinkgem.jeesite.modules.source.dao.EleSourceGroupDao;
import com.thinkgem.jeesite.modules.source.entity.EleSource;
import com.thinkgem.jeesite.modules.publish.service.EleTempletPublishService;
import com.thinkgem.jeesite.modules.source.dao.EleSourceDao;

/**
 * 节目包管理Service
 * @author ZC
 * @version 2017-05-25
 */
@Service
@Transactional(readOnly = true)
public class EleSourceGroupService extends CrudService<EleSourceGroupDao, EleSourceGroup> {

	@Autowired
	private EleSourceDao eleSourceDao;
	@Autowired
	private EleSourceGroupDao eleSourceGroupDao;

	
	public EleSourceGroup get(String id) {
		EleSourceGroup eleSourceGroup = super.get(id);
		eleSourceGroup.setEleSourceList(eleSourceDao.findList(new EleSource(eleSourceGroup)));
		return eleSourceGroup;
	}
	public EleSourceGroup getGroupById(String id) {
		EleSourceGroup eleSourceGroup = eleSourceGroupDao.getGroupById(id);
		if(eleSourceGroup != null) {
			eleSourceGroup.setEleSourceList(eleSourceDao.findList(new EleSource(eleSourceGroup)));
		}
		return eleSourceGroup;
	}
	
	public List<EleSourceGroup> findList(EleSourceGroup eleSourceGroup) {
		return super.findList(eleSourceGroup);
	}
	public List<EleSource> findSourcesOnEquipment(String equipmentId,String status) {
		return eleSourceDao.findSourcesOnEquipment(equipmentId, status);
	}
	public List<EleSource> findSourceOnEquipmentByType(String equipmentId,String sourceStatus,String sourceType) {
		return eleSourceDao.findSourceOnEquipmentByType(equipmentId, sourceStatus, sourceType);
	}
	public List<EleSourceGroup> findListByType(String type,String remarks) {
		return eleSourceGroupDao.findListByType(type,remarks);
	}
	public Page<EleSourceGroup> findPage(Page<EleSourceGroup> page, EleSourceGroup eleSourceGroup) {
		return super.findPage(page, eleSourceGroup);
	}
	public Page<EleSourceGroup> findPageForCheck(Page<EleSourceGroup> page, List<User> users,String status) {
		page.setList(eleSourceGroupDao.findEleEleSourceGroupForCheck(users, status));
		return page;
	}
	public Page<EleSourceGroup> findPageForCheckForProgram(EleSourceGroup eleSourceGroup, Page<EleSourceGroup> page, List<User> users,String status) {
		eleSourceGroup.setPage(page);
		page.setList(eleSourceGroupDao.findEleEleSourceGroupForCheckForProgram(eleSourceGroup, users, status));
//		page.setCount(page.getList().size());

		return page;
	}
	
	public List<EleSourceGroup> findPageForCheckForProgram(List<User> users,String status) {
		return eleSourceGroupDao.findEleEleSourceGroupForCheckForProgram(users, status);
	}
	@Transactional(readOnly = false)
	public int updateSource(EleSource entity){
		return eleSourceDao.update(entity);
	}
	@Transactional(readOnly = false)
	public int deleteSource(EleSource entity){
		return eleSourceDao.delete(entity);
	}
	@Transactional(readOnly = false)
	public void deleteFromDB(String groupId){
		eleSourceGroupDao.deleteFromDB(groupId);
	}
	public EleSource getSourceById(String id){
		return eleSourceDao.get(id);
	}
	@Transactional(readOnly = false)
	public void save(EleSourceGroup eleSourceGroup) {
		super.save(eleSourceGroup);
		for (EleSource eleSource : eleSourceGroup.getEleSourceList()){
			if (eleSource.getId() == null){
				continue;
			}
			if (EleSource.DEL_FLAG_NORMAL.equals(eleSource.getDelFlag())){
				if (StringUtils.isBlank(eleSource.getId())){
					eleSource.setGroup(eleSourceGroup);
					eleSource.preInsert();
					eleSourceDao.insert(eleSource);
				}else{
					eleSource.preUpdate();
					eleSourceDao.update(eleSource);
				}
			}else{
				eleSourceDao.delete(eleSource);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(EleSourceGroup eleSourceGroup) {
		super.delete(eleSourceGroup);
		eleSourceDao.delete(new EleSource(eleSourceGroup));
	}
	@Transactional(readOnly = false)
	public int updateGroup(EleSourceGroup entity){
		return eleSourceGroupDao.update(entity);
	}
	@Transactional(readOnly = false)
	public int bathcUpdateGroupStatus(String groups,String status,String checker, String comment){
		return eleSourceGroupDao.batchUpdateGroupStatus(groups, status,checker, comment);
	}

	// Edited by jyz (2018.5)
	public boolean resourceFileExistById(String id) {
		String basePath = Global.getUserfilesBaseDir();
		String zipPath = basePath + "/theme/source/"+id+"/" + id + ".zip";
		File zipFile = new File(zipPath);
		return zipFile.exists();
	}
}